﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Manager_Links : System.Web.UI.PageEx
{
    private string strPageCode = "Q0000";
    private string strLoginResult = string.Empty;

    private int iPageCount = 0;
    private int iRecordCount = 0;
    private string strQuery = string.Empty;
    private int iSelectId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!DalEx.DbManagerEx.CheckLoginRight(UserType, UserId, strPageCode, ref strLoginResult))
        {
            FineUI.Alert.Show(strLoginResult, FineUI.MessageBoxIcon.Warning);
            Response.Redirect("Login.aspx");
        }
        if (!Page.IsPostBack)
        {
            DataBinds();
            btnReset.OnClientClick = sfObject.GetResetReference();
        }
    }
    private void DataBinds()
    {
        DataSet ds = WebBase.Comm.SelectByPage("dbLinks", "*", strQuery, "lId", "lId", 0, 1, gridList.PageSize, gridList.PageIndex + 1, out iPageCount, out iRecordCount);
        gridList.RecordCount = iRecordCount;
        gridList.DataSource = ds;
        gridList.DataBind();
    }
    protected void gridDataList_RowDataBound(object sender, FineUI.GridRowEventArgs e)
    {
        DataRowView drv = (DataRowView)gridList.Rows[e.RowIndex].DataItem;
        short sType = Convert.ToInt16(drv["lType"]);
        if (sType == 1)
        {
            Literal litShowLogo = (Literal)gridList.Rows[e.RowIndex].FindControl("litShowLogo");
            litShowLogo.Text = "<img src=\"" + Convert.ToString(drv["lLogo"]) + "\" title=\"" + Convert.ToString(drv["lTitle"]) + "\" />";
        }
    }
    protected void gridList_RowCommand(object sender, FineUI.GridCommandEventArgs e)
    { 
        
    }
    protected void gridList_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
    {
        gridList.PageIndex = e.NewPageIndex;
        DataBinds();
    }
    protected void gridList_RowDoubleClick(object sender,EventArgs e)
    {
        List<int> lstSelectRowId = WebBase.Comm.GetGridSeleteRowId(gridList);
        if (lstSelectRowId.Count >= 1)
        {
            iSelectId = lstSelectRowId[0];
            Model.DbLinks dbLinks = DalEx.DbLinksEx.Select(iSelectId);
            if (dbLinks != null)
            {
                hfLinkId.Text = iSelectId.ToString();
                txtTitle.Text = dbLinks.LTitle;
                cboxType.Checked = Convert.ToBoolean(dbLinks.LType);
                txtUrl.Text = dbLinks.LUrl;
            }
        }
    }
    protected void btnSelectDelete_Click(object sender, EventArgs e)
    {
        string strResult = string.Empty;
        List<int> lstSelectRowId = WebBase.Comm.GetGridSeleteRowId(gridList);
        if (lstSelectRowId.Count >= 1)
        {
            string[] SelectIdArray = new string[lstSelectRowId.Count];
            int iIndexTemp =0;
            foreach (int iLinkId in lstSelectRowId)
                SelectIdArray[iIndexTemp] = iLinkId.ToString();
            if (DalEx.DbLinksEx.Delete(" and lId in (" + string.Join(",", SelectIdArray) + ")", ref strResult) > 0)
            {
                FineUI.Alert.Show(strResult, "信息提示", FineUI.MessageBoxIcon.Information);
                sfObject.GetResetReference();
                DataBinds();
            }
            else
            {
                FineUI.Alert.Show(strResult, "信息提示", FineUI.MessageBoxIcon.Error);
            }
        }
        else
        {
            FineUI.Alert.Show("请选择要删除的内容！", "信息提示", FineUI.MessageBoxIcon.Warning);
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int iLinkFrom = 1;
        int.TryParse(hfLinkId.Text.Trim(), out iSelectId);
        string strTitle = SqlHelper.String(txtTitle.Text);
        short sType = Convert.ToInt16(cboxType.Checked);
        string strUrl = SqlHelper.String(txtUrl.Text);
        string strLogo = string.Empty;
        if (sType == 1)
        {
            string strErrorMsg = string.Empty;
            strLogo = WebBase.Comm.GetFilePath(fuLogo, Server.MapPath("~/upload/Links/"), ResolveUrl("~/upload/Links/"), false, 1, AllowUpFileExt, ref strErrorMsg);
        }
        Model.DbLinks dbLinks = new Model.DbLinks();
        dbLinks.LAddTime = DateTime.Now;
        dbLinks.LViews = 0;
        dbLinks.LLogo = strLogo;
        dbLinks.LFrom = iLinkFrom;
        if (iSelectId > 0)
        {
            dbLinks = DalEx.DbLinksEx.Select(iSelectId);
            strLogo = string.IsNullOrEmpty(strLogo) ? dbLinks.LLogo : strLogo;
        }
        else
        {
            if (sType == 1 && string.IsNullOrEmpty(strLogo))
            {
                FineUI.Alert.Show("图片链接，请选择LOGO图标！", "信息提示", FineUI.MessageBoxIcon.Error);
                return;
            }
        }
        dbLinks.LTitle = strTitle;
        dbLinks.LType = sType;
        dbLinks.LUrl = strUrl;
        dbLinks.LLogo = sType == 0 ? string.Empty : strLogo;

        string strResult = string.Empty;
        if (DalEx.DbLinksEx.Insert(dbLinks, ref strResult) > 0)
        {
            DataBinds();
            FineUI.Alert.Show(strResult, "信息提示", FineUI.MessageBoxIcon.Information);
        }
        else
        {
            FineUI.Alert.Show(strResult, "信息提示", FineUI.MessageBoxIcon.Error);
        }
    }
}
